package com.shanghaiyimu.yimiyangche.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.activity.ShopContentActivity;
import com.shanghaiyimu.yimiyangche.bean.ShopBean;
import com.shanghaiyimu.yimiyangche.bean.WashCarBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/5.
 */

public class MyWashListViewAdapter extends BaseAdapter {
    private Context context;
    private List<WashCarBean> data;

    public MyWashListViewAdapter(Context context,List<WashCarBean> data){
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data != null?data.size():0;
    }

    @Override
    public WashCarBean getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null){
            view = LayoutInflater.from(context).inflate(R.layout.wash_listview_item,viewGroup,false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }
        WashCarBean washCarBean = data.get(i);
        String name = washCarBean.getName();
        holder.mTextViewName.setText(name);
        ShopBean shopBean ;
        //模拟数据
        List<ShopBean> data = null;
        data = new ArrayList<>();
        for (int j = 0; j < 3; j++) {
            shopBean = new ShopBean();
            shopBean.setName("金花北路宜牧店"+j);
            shopBean.setPrice(j+"0");
            data.add(shopBean);
        }

        MyWashListViewItemAdapter adapter = new MyWashListViewItemAdapter(context,data);

        holder.mlistView.setAdapter(adapter);
        setListViewHeightBasedOnChildren(holder.mlistView);
        final List<ShopBean> finalData = data;
        holder.mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(context,ShopContentActivity.class);
//                Bundle bundle = new Bundle();
//                bundle.putString("key", finalData.get(i).getName().toString());
//                intent.putExtra("name",bundle);
                context.startActivity(intent);

            }
        });
        return view;
    }
    class ViewHolder{
        ListView mlistView;
        TextView mTextViewName;
        public ViewHolder(View view){
            mTextViewName = (TextView) view.findViewById(R.id.wash_textview_head);

            mlistView = (ListView) view.findViewById(R.id.wash_listview_item);
        }

    }
    /**
     *
     * 此方法是本次listview嵌套listview的核心方法：计算parentlistview item的高度。
     * 如果不使用此方法，无论innerlistview有多少个item，则只会显示一个item。
     **/
    public void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {        return;    }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}
